﻿using Pay.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pay.DTO;
using Pay.TO;
using Pay.Core;

namespace Pay.Token.Controllers
{
    /// <summary>
    /// 购物车
    /// </summary>
    [AppAuthorize("Member")]
    public class CartController : GeneralController {
        private IProductService _iProductService;
        private ICartService _iCartService;
        private IShopInfoService _iShopInfoService;
        public CartController(IProductService iProductService, ICartService iCartService, IShopInfoService iShopInfoService) {
            _iCartService = iCartService;
            _iProductService = iProductService;
            _iShopInfoService = iShopInfoService;
        }
        // GET: Cart
        public ActionResult Index()
        {
            var cart = _iCartService.GetTokenCartList(UserId);
            ViewBag.img_url = RequestMethod.token_imgurl;
            return View(cart);
        }
        /// <summary>
        /// 加入购物车
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddCart(AddCartModel model) {
            if (!ModelState.IsValid)
                return Json(ResultToJson.Json<bool>(ModelError));
            model.merchant_id = UserId;
           int result = _iCartService.AddTokenCart(model);
            return Json(ResultToJson.Json(result >0 ? " 加入成功" : "加入失败", result), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 删除购物车
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public JsonResult DelCart(List<long> ids) {
            int result = _iCartService.DeleteTokenCart(ids,UserId);
            return Json(ResultToJson.Json(result > 0 ? " 删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 编辑购物车
        /// </summary>
        /// <param name="cart_id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public JsonResult UpdateCart(long cart_id,int type) {
            int result = _iCartService.UpdateCart(cart_id, UserId,type);
            return Json(ResultToJson.Json(result > 0 ? " 保存成功" : "加入失败", result), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 购买
        /// </summary>
        /// <param name="cart_id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public JsonResult AddBuy(long cart_id, int type) {
            int result = _iCartService.UpdateCart(cart_id, UserId, type);
            return Json(ResultToJson.Json(result > 0 ? " 保存成功" : "加入失败", result), JsonRequestBehavior.AllowGet);
        }
    }
}